﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client.Services;
using KoPortal.Sys.Data.MySQL;

namespace KoServices
{
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [BasicHttpBindingServiceMetadataExchangeEndpoint]
    //[BasicHttpBindingServiceMetadataExchangeEndpointAttribute]
    //[AspNetCompatibilityRequirements]    
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class KoServices : IKoServices
    {
        public string GetCurentWebUrl()
        {
            return SPContext.Current.Web.Url;
        }

        // [OperationBehavior]
        public byte [] GetMySqlSrvList()
        {
            Exception err = null;
            byte[] b = null;
            
            //______________________
            // Get My Sql Srv List :
            DataTable dt = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                err = DBKoLocal.GetMySqlSrvList(ref dt);
            });

            //__________________________________
            // Make data table individual rows :
            /*
            DataTable dt = new DataTable("tbl1");
            DataRow dr;

            dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
            dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
            dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("BooleanValue", typeof(bool)));

            for (int i = 1; i <= 5; i++)
            {

                dr = dt.NewRow();

                dr[0] = i;
                dr[1] = "Item " + i.ToString();
                dr[2] = DateTime.Now;
                dr[3] = (i % 2 != 0) ? true : false;

                dt.Rows.Add(dr);
            }
            */

            if (err == null && dt!=null)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (System.IO.MemoryStream xmlStream = new System.IO.MemoryStream())
                    {
                        dt.TableName = "tbl";
                        dt.WriteXml(xmlStream, XmlWriteMode.WriteSchema);
                        //ret = xmlStream.ToString();
                        xmlStream.Position = 0;
                        b = xmlStream.ToArray();
                    }
                });
            }
            return b;
        }
    }
}
